package com.xiaxueqi.web.shop;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.niagara.context.SecurityContext;
import com.niagara.security.auth.entity.UserDetail;
import com.niagara.service.base.BaseService;
import com.niagara.web.struts2.Struts2Utils;
import com.xiaxueqi.entity.book.Address;
import com.xiaxueqi.entity.book.BookForsaleExt;
import com.xiaxueqi.entity.book.OrderDetail;

/**
 * 购物车的设计
 * 
 * @author liangThink
 * 
 */
@Results( {
		@Result(name = "list", location = "/ajax/shopping-cart-list.action", type = "redirect"),
		@Result(name = "dellist", location = "/ajax/shopping-cart-dellist.action", type = "redirect") })
public class ShoppingCartAction {
	@Autowired
	private BaseService baseService;
	private Integer productId;
	private Integer quantity;
	private HttpSession session = Struts2Utils.getSession();

	private ShopCart shopCart;

	private List<Address> address;
	private UserDetail detail = SecurityContext.getUserDetail();
	Integer userId = detail != null ? Integer.valueOf(detail.getUserId()
			.intValue()) : null;

	public String execute() throws Exception {
		this.setShopCart();
		return "success";
	}

	public String add() throws Exception {
		this.setShopCart();
		if (null == shopCart.getOneProduct(productId)) {
			BookForsaleExt bookForsale = new BookForsaleExt();
			bookForsale.setBookId(productId);
			bookForsale = baseService.get(BookForsaleExt.class, bookForsale,
					"select_by_condition");
			Double price = bookForsale.getPrice();
			OrderDetail detail = new OrderDetail();
			detail.setProductId(productId);
			detail.setProductName(bookForsale.getBookName());
			detail.setUnitPrice(price == null ? 0 : price);
			detail.setQuantity(quantity);
			detail.setProductType("book");
			detail.setOwnerId(bookForsale.getOwnerId());
			shopCart.add(detail);
		} else {
			shopCart.add(productId, quantity);
		}
		session.setAttribute("shopCart", shopCart);
		return null;
	}

	public String set() throws Exception {
		this.setShopCart();
		if (quantity == 0) {
			this.delete();
		} else {
			shopCart.set(productId, quantity);
			session.setAttribute("shopCart", shopCart);
		}
		return null;
	}

	/**
	 * 删除购物车中一种商品
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception {
		this.setShopCart();
		OrderDetail oneProduct = shopCart.getOneProduct(productId);
		shopCart.remove(oneProduct);
		session.setAttribute("shopCart", shopCart);
		return null;
	}

	public String clear() throws Exception {
		this.setShopCart();
		session.removeAttribute("shopCart");
		this.setShopCart();
		return null;
	}

	public String addOne() throws Exception {
		this.setShopCart();
		shopCart.add(productId);
		session.setAttribute("shopCart", shopCart);
		return null;
	}

	public String deleteOne() throws Exception {
		this.setShopCart();
		shopCart.remove(productId);
		session.setAttribute("shopCart", shopCart);
		return null;
	}

	public String change() throws Exception {
		this.setShopCart();
		shopCart.change(productId);
		session.setAttribute("shopCart", shopCart);
		return null;
	}

	@SuppressWarnings("unchecked")
	public String out() throws Exception {
		shopCart = (ShopCart) session.getAttribute("shopCart");
		if (userId != null) {
			Map map = new HashMap();
			map.put("userId", userId);
			map.put("start", 0);
			map.put("pageSize", 3);
			map.put("orderByClause", "create_time desc");
			address = baseService.find(Address.class, map);
		}
		return "out";
	}

	public String list() throws Exception {
		return "list";
	}

	public String deleteList() throws Exception {
		return "dellist";
	}

	public Integer getProductId() {
		return productId;
	}

	public void setProductId(Integer productId) {
		this.productId = productId;
	}

	public Integer getQuantity() {
		return quantity;
	}

	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}

	public ShopCart getShopCart() {
		return shopCart;
	}

	public void setShopCart() {
		if (session.getAttribute("shopCart") == null) {
			session.setAttribute("shopCart", new ShopCart());
		}
		shopCart = (ShopCart) session.getAttribute("shopCart");
	}

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

	public List<Address> getAddress() {
		return address;
	}

	public Integer getUserId() {
		return userId;
	}

	public UserDetail getDetail() {
		return detail;
	}

	public static void main(String[] args) {
		ShoppingCartAction action = new ShoppingCartAction();
		System.out.println(action.productId);
	}
}
